//
//  HMStatusCell.swift
//  Faner_Weibo
//
//  Created by 范英强 on 16/2/1.
//  Copyright © 2016年 范英强. All rights reserved.
//

import UIKit
import SnapKit
let HMStatusCellMargin: CGFloat = 10

class HMStatusCell: UITableViewCell {
    
    var toolBarConstraints: Constraint?
    
    var statusViewModel: HMStatusViewModel?{
        didSet{
            originalView.statusViewModel = statusViewModel
            toolBar.statusViewModel = statusViewModel
            
            toolBarConstraints?.uninstall()
            if statusViewModel?.status?.retweeted_status != nil {
                retweetView.hidden = false
                retweetView.statusViewModel = statusViewModel
                
                toolBar.snp_updateConstraints(closure: { (make) -> Void in
                    self.toolBarConstraints = make.top.equalTo(retweetView.snp_bottom).constraint
                })
                
                
            }else{
                retweetView.hidden = true
                toolBar.snp_updateConstraints(closure: { (make) -> Void in
                    self.toolBarConstraints = make.top.equalTo(originalView.snp_bottom).constraint
                })
            }
        }
    }

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI(){
        contentView.backgroundColor = UIColor(white: 0.95, alpha: 1)
        contentView.addSubview(originalView)
        contentView.addSubview(retweetView)
        contentView.addSubview(toolBar)
        
        originalView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(contentView)
            make.trailing.equalTo(contentView)
            make.top.equalTo(contentView).offset(8)
        }
        
        retweetView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(originalView)
            make.trailing.equalTo(originalView)
            make.top.equalTo(originalView.snp_bottom)
        }
        
        toolBar.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(contentView)
            self.toolBarConstraints = make.top.equalTo(retweetView.snp_bottom).constraint
            make.height.equalTo(35)
            make.width.equalTo(originalView)
        }
        
        contentView.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(toolBar)
            make.top.equalTo(self)
            make.leading.equalTo(self)
            make.trailing.equalTo(self)
        }
    }
    
    // MARK: - 懒加载控件
    
    private lazy var originalView: HMStatusOriginalView = {
        let view = HMStatusOriginalView()
        view.backgroundColor = UIColor.whiteColor()
        return view
    }()
    //底部toolbar
    private lazy var toolBar: HMStatusToolBar = HMStatusToolBar()
    
    private lazy var retweetView: HMStatusRetweetView = HMStatusRetweetView()
}
